package cn.tedu._07vehicle.controller;

import cn.tedu._07vehicle.base.response.Result;
import cn.tedu._07vehicle.base.response.StatusCode;
import cn.tedu._07vehicle.mapper.VehicleMapper;
import cn.tedu._07vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._07vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._07vehicle.pojo.entity.Vehicle;
import cn.tedu._07vehicle.pojo.vo.UserVO;
import cn.tedu._07vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author tune_the last
 * @date 2025/11/14 - 16:05
 */
@Slf4j
@Validated
@Tag( name = "车辆管理模块")
@RequestMapping("/v1/vehicle")
@RestController
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @ApiOperationSupport( order = 10)
    @Operation(summary = "添加车辆")
    @PostMapping("/add")
    public Result addVehicle(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session){
        log.debug("请求获取参数："+ vehicleAddParam);
        //登录校验
        UserVO userVO = (UserVO)session.getAttribute("user");
        if (userVO == null){
            return Result.fail(StatusCode.NOT_LOGIN);
        }

        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setUserId(userVO.getId());
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        int i =vehicleMapper.insertVehicle(vehicle);
        if (i > 0){
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperationSupport( order = 20)
    @Operation(summary = "查询车辆")
    @Parameters({
            @Parameter(name = "brand", description = "车辆品牌"),
            @Parameter(name = "license", description = "车辆号"),
            @Parameter(name = "vehicleListQuery", hidden = true)
    })
    @GetMapping("/list")
    public Result listVehicle(VehicleListQuery vehicleListQuery, HttpSession session){
        log.debug("请求获取参数："+ vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return Result.fail(StatusCode.NOT_LOGIN);
        }
        List<VehicleListVO> list = vehicleMapper.selectByCondition(vehicleListQuery, userVO.getId());

        return Result.ok(list);
    }

    @ApiOperationSupport(order = 30)
    @Operation(summary = "删除车辆")
    @PostMapping("/del/{id}")
    public Result delVehicle(@PathVariable("id") @Range(min = 100, max = 500) Long vehicleId, HttpSession session){
        //手动抛出异常，演示全局异常处理器的使用;
        if (vehicleId > 0){
            throw new IllegalArgumentException("车辆id不能大于0");
        }
        log.debug("请求获取参数："+ vehicleId);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return Result.fail(StatusCode.NOT_LOGIN);
        }
        int i = vehicleMapper.deleteVehicle(vehicleId, userVO.getId());
        if (i > 0){
            return Result.ok();
        }
        return Result.fail();
    }
}
